//package com.xhwl.centiotdata.handler.device.impl;
//
//import com.alibaba.fastjson.JSONObject;
//import com.xhwl.centiotdata.handler.device.DeviceIntegrationFactory;
//import com.xhwl.centiotdata.handler.device.DeviceIntegrationHandler;
//import com.xhwl.centiotdata.mq.producer.GeneralProducer;
//import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
//import com.xhwl.centiotdata.pojo.dto.keytop.DeviceKeyTopDTO;
//import com.xhwl.centiotdata.service.cent.IDeviceCentService;
//import com.xhwl.centiotdata.suppliersystem.keytop.DeviceKeyTopHandler;
//import com.xhwl.common.constant.cent.alarm.AlarmConstant;
//import com.xhwl.common.dto.cent.device.DeviceDTO;
//import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
//import com.xhwl.common.pojo.cent.alarm.AlarmManagement;
//import com.xhwl.common.pojo.cent.device.Device;
//import com.xhwl.common.query.cent.device.DeviceQuery;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.CollectionUtils;
//
//import java.util.Date;
//import java.util.List;
//import java.util.stream.Collectors;
//
///**
// * @author tangwei
// * @description 科拓集成handler
// * @create 2022-10-21 15:37
// */
//@Slf4j
//@Service
//public class DeviceIntegrationKeyTopHandler extends DeviceIntegrationHandler {
//
//    @Autowired
//    private DeviceKeyTopHandler deviceKeyTopHandler;
//    @Autowired
//    GeneralProducer generalProducer;
//    @Autowired
//    private IDeviceCentService deviceCentService;
//
//    @Override
//    public void afterPropertiesSet() throws Exception {
//        DeviceIntegrationFactory.register(72, this);
//    }
//
//    @Override
//    public void pullDevice(DeviceIntegrationConfigDTO configDTO) {
//        // 拉取数据
//        List<DeviceKeyTopDTO> deviceKeyTopDTOList = deviceKeyTopHandler.pullDevice(configDTO);
//        log.info("deviceKeyTopDTOList"+deviceKeyTopDTOList);
//        if (CollectionUtils.isEmpty(deviceKeyTopDTOList)) {
//            return;
//        }
//
//        List<DeviceDTO> deviceDTOList = this.handleDevice(configDTO, deviceKeyTopDTOList);
//        log.info("开始转存设备数据"+deviceDTOList);
//        // 存储业务中台
//        deviceCentService.asyncUpdateDevice(deviceDTOList);
//    }
//
//    public void pullDoorOnlineStatus(DeviceIntegrationConfigDTO configDTO){
//        // 拉取数据
//        List<DeviceKeyTopDTO> deviceKeyTopDTOList = deviceKeyTopHandler.pullDevice(configDTO);
//        if (CollectionUtils.isEmpty(deviceKeyTopDTOList)) {
//            return;
//        }
//        // 转换数据
//        List<Device> deviceDTOList = this.handleDeviceOnlineStatus(configDTO, deviceKeyTopDTOList);
//        List<Device> offLineDevices = deviceDTOList.stream().filter(s->s.getDeviceStatus().equals(DeviceOnlineStatusEnum.OFFLINE.getId())).collect(Collectors.toList());
//        offlineDeviceAlarm(offLineDevices,configDTO.getProjectId());
//        // 存储业务中台
//        deviceCentService.asyncUpdateOnlineStatus(deviceDTOList);
//
//    }
//
//    private List<DeviceDTO> handleDevice(DeviceIntegrationConfigDTO configDTO, List<DeviceKeyTopDTO> deviceKeyTopDTOList) {
//        return deviceKeyTopDTOList.stream().map(deviceKeyTopDTO -> {
//            DeviceDTO deviceDTO = new DeviceDTO();
//            deviceDTO.setProjectId(configDTO.getProjectId());
//            deviceDTO.setProductId(configDTO.getProductId());
//            deviceDTO.setThirdDeviceId(deviceKeyTopDTO.getCode());
//            deviceDTO.setCategoryId(configDTO.getCategoryEnum().getId());
//            deviceDTO.setDeviceTypeId(configDTO.getDeviceType());
//            deviceDTO.setName(deviceKeyTopDTO.getName());
//            deviceDTO.setAttributeJson(new JSONObject());
//            deviceDTO.setOnlineStatus(deviceKeyTopDTO.getState() ? DeviceOnlineStatusEnum.ONLINE.id : DeviceOnlineStatusEnum.OFFLINE.id);
//            return deviceDTO;
//        }).collect(Collectors.toList());
//    }
//
//    private List<Device> handleDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO, List<DeviceKeyTopDTO> doorHaiKangDTOList) {
//        return doorHaiKangDTOList.stream().map(deviceKeyTopDTO -> {
//            Device device = new Device();
//            device.setProjectId(configDTO.getProjectId());
//            device.setDeviceTypeId(configDTO.getDeviceType());
//            device.setThirdDeviceId(deviceKeyTopDTO.getCode());
//            device.setOnlineStatus(deviceKeyTopDTO.getState() ? DeviceOnlineStatusEnum.ONLINE : DeviceOnlineStatusEnum.OFFLINE);
//            return device;
//        }).collect(Collectors.toList());
//    }
//
//    private void offlineDeviceAlarm(List<Device> deviceDTOList,Integer projectId){
//        List<Device> offLineDevices = deviceDTOList.stream().filter(s->s.getDeviceStatus().equals(DeviceOnlineStatusEnum.OFFLINE.getId())).collect(Collectors.toList());
//        if (!CollectionUtils.isEmpty(offLineDevices)) {
//            DeviceQuery deviceQuery = new DeviceQuery();
//            deviceQuery.setProjectId(projectId);
//            deviceQuery.setThirdDeviceIdList(offLineDevices.stream().map(Device::getThirdDeviceId).collect(Collectors.toList()));
//            List<DeviceDTO> deviceList = deviceCentService.listDevice(null, deviceQuery);
//            offLineDevices.forEach(device->{
//                AlarmManagement alarmManagement = new AlarmManagement();
//                DeviceDTO deviceDTO = deviceList.stream().filter(s-> StringUtils.equals( s.getThirdDeviceId(), device.getThirdDeviceId()))
//                        .findAny().orElse(null);
//                if(deviceDTO != null) {
//                    //组装公共部分数据
//                    alarmManagement.setProjectId(projectId);
//                    alarmManagement.setAlarmTime(new Date());
//                    alarmManagement.setDeviceId(device.getId());
//                    //设备离线告警
//                    alarmManagement.setAlarmInventoryCode("17798944");
//                    generalProducer.send(AlarmConstant.KEYTOP_ALARM_TOPIC, alarmManagement);
//                }
//            });
//        }
//    }
//
//}
